package com.yzt.minemodule.wallet.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.yzt.zhuju.base.BaseFragment;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.wallet.WalletDetialList;
import com.yzt.zhuju.network.entity.mine.wallet.WalletScreenEntry;
import com.yzt.minemodule.R;
import com.yzt.minemodule.wallet.WalletDetailInfoActivity;
import com.yzt.minemodule.wallet.adapter.WalletDetialListAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 收支列表
 */
public class WalletDetailListFragment extends BaseFragment {

    private static final int INTENT_CODE = 110;

    private int type;
    private RecyclerView mListRv;
    private WalletDetialListAdapter mWalletDetailListAdapter;

    private RelativeLayout mLetterLayout;
    private TextView tvStickyHeaderView;
    private TextView tvContent;

    private List<WalletDetialList> mDetailList;
    protected List<String> groupList = new ArrayList<>();

    private final List<WalletScreenEntry> mScreenList = new ArrayList<>();

    private String mFilterId = "1";

    public static WalletDetailListFragment newInstance(int type) {
        WalletDetailListFragment fragment = new WalletDetailListFragment();
        fragment.type = type;
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_wallet_detail_list;
    }

    @Override
    public void installViews() {
        mLetterLayout = findViewById(R.id.letter_layout);
        mLetterLayout.setVisibility(View.GONE);
        tvStickyHeaderView = findViewById(R.id.first_letter_tv);
        tvContent = findViewById(R.id.detail_tv);

        mDetailList = new ArrayList<>();
        mListRv = findViewById(R.id.list_rv);
        mWalletDetailListAdapter = new WalletDetialListAdapter(mDetailList);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mListRv.setLayoutManager(mLinearLayoutManager);
        mListRv.setAdapter(mWalletDetailListAdapter);

        mListRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View stickyInfoView = recyclerView.findChildViewUnder(mLetterLayout.getMeasuredWidth() >> 1, 5);
                if (stickyInfoView != null && stickyInfoView.getContentDescription() != null) {
                    String content = String.valueOf(stickyInfoView.getContentDescription());
                    String[] split = content.split("#");
                    tvStickyHeaderView.setText(split[0]);
                    tvContent.setText(split[1]);
                }
                View transInfoView = recyclerView.findChildViewUnder(mLetterLayout.getMeasuredWidth() >> 1, mLetterLayout.getMeasuredHeight() + 1);
                if (transInfoView != null && transInfoView.getTag() != null) {
                    int transViewStatus = (int) transInfoView.getTag();
                    int dealtY = transInfoView.getTop() - mLetterLayout.getMeasuredHeight();
//                    if (transViewStatus == WalletDetialList.TYPE_HEADER) {
//                        if (transInfoView.getTop() > 0) {
//                            mLetterLayout.setTranslationY(dealtY);
//                        } else {
//                            mLetterLayout.setTranslationY(0);
//                        }
//                    } else
                    if (transViewStatus == WalletDetialList.TYPE_CONTENT) {
                        mLetterLayout.setTranslationY(0);
                    }
                }
            }
        });

        // 列表监听
        mWalletDetailListAdapter.setOnItemClickListener((adapter, view, position) -> {
            WalletDetialList item = (WalletDetialList) adapter.getItem(position);
            if (item.getItemType() == WalletDetialList.TYPE_CONTENT) {
                WalletDetailInfoActivity.start(mActivity, type, item, INTENT_CODE);
            }
        });

        // 列表加载
        mWalletDetailListAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            initData();
        });
    }

    @Override
    public void netWorkRequest() {
        initData();
        //getFilter();
    }

    /**
     * 加载过滤条件弹窗
     */
    public void sendFilterDialog(String extraId) {
//        WalletLabelDialog mScreenDialogFragment = (WalletLabelDialog) BottomBaseDialog.showDialog(mActivity, WalletLabelDialog.class);
//        mScreenDialogFragment.setList(mScreenList);
//        mScreenDialogFragment.setListener((position, entry) -> {
//            if (position >= mScreenList.size()) {
//                return;
//            }
//            for (WalletScreenEntry walletScreenEntry : mScreenList) {
//                walletScreenEntry.setCheck(false);
//            }
//            mScreenList.get(position).setCheck(true);
//            mFilterId = mScreenList.get(position).getId();
//            mCurrentPage = 1;
//            initData();
//        });
        mFilterId = extraId;
        mCurrentPage = 1;
        initData();
    }

    /**
     * 获取过滤条件
     */
    public void getFilter() {
        BizLayer.getInstance().getMineModule().getFilterList(type, new AresSubscriber<List<WalletScreenEntry>>() {
            @Override
            protected void onSuccess(List<WalletScreenEntry> list) {
                mScreenList.addAll(list);
            }
        });
    }

    /**
     * 获取收支列表数据
     */
    public void initData() {
        BizLayer.getInstance().getMineModule().walletList(mFilterId, type, mCurrentPage, PAGE_SIZE, new AresSubscriber<List<WalletDetialList>>() {
            @Override
            protected void onSuccess(List<WalletDetialList> list) {

                if (mCurrentPage == 1) {
                    groupList.clear();
                    mLetterLayout.setVisibility(View.GONE);
                    mWalletDetailListAdapter.getData().clear();
                    if (list.size() == 0) {
                        mWalletDetailListAdapter.setEmptyView(getEmptyView(R.mipmap.icon_empty_friend, getString(R.string.page_empty)));
                    } else {
                        // mLetterLayout.setVisibility(View.VISIBLE);
                        tvStickyHeaderView.setText(list.get(0).getRecordDay());
                        tvContent.setText(list.get(0).getMessage());
                        mWalletDetailListAdapter.removeEmptyView();
                    }
                }
                List<WalletDetialList> copyList = new ArrayList<>();

                for (int i = 0; i < list.size(); i++) {
                    WalletDetialList walletDetail = list.get(i);
                    String groupName = walletDetail.getRecordDay();
//                    if (!groupList.contains(groupName)) {
//                        copyList.add(WalletDetialList.copy(walletDetail, WalletDetialList.TYPE_HEADER));
//                    }
                    copyList.add(WalletDetialList.copy(walletDetail, WalletDetialList.TYPE_CONTENT));
                    if (!groupList.contains(groupName)) {
                        groupList.add(groupName);
                    }
                }
                mWalletDetailListAdapter.addData(copyList);
                mWalletDetailListAdapter.getLoadMoreModule().loadMoreComplete();
                if (list.size() < PAGE_SIZE) {
                    mWalletDetailListAdapter.getLoadMoreModule().loadMoreEnd();
                }
                mDetailList = mWalletDetailListAdapter.getData();
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (mCurrentPage == 1) {
                    mLetterLayout.setVisibility(View.GONE);
                    mWalletDetailListAdapter.setEmptyView(getEmptyView(info));
                }
            }
        });
    }

    public void setIdRemove(String id) {
        mCurrentPage = 1;
        initData();
        int postion = 0;
        for (int i = 0; i < mDetailList.size(); i++) {
            if (TextUtils.equals(id, mDetailList.get(i).getId())) {
                postion = i;
            }
        }
        mListRv.smoothScrollToPosition(postion);
    }
}
